/******************************************************************************
Author: Akshay Dixit
Date Created: May 23, 2018
Date last modified: June 11, 2020
File Name: 5_in.do
Project: T4D Indonesia 
Purpose: Analysis of intermediate outcomes (Facility data)
******************************************************************************/

/* LIST OF INTERMEDIATE OUTCOMES & THE ASSOCIATED VARIABLES
	
	Note that the following codes apply:
		V. Other, specify _________
		X. REFUSED
		Y. DON'T KNOW
		
		95. Other, specify _________
		97. REFUSED
		98. DON'T KNOW
		99. MISSING
		
	Note:	The sample has 200 facilities. But there were 201 observations in the facility dataset. 
			This is because are 2 facilities in Enumeration Area (EA) 114, facility code 11411 (Puskemas Ulugalung) and 11455 (Puskesmas Kota).
			At baseline, village Onto fell under the jurisdiction of Puskesmas Ulugalung. Since the end of 2015, it falls
			under Puskesmas Kota. 
			When the intervention was implemented, Onto was under Kota.
			Data was collected from both facilities. Ulugalung (11411) is dropped from the analysis. 
	

Category 2. Improved facility access (transportation, new facility, longer facility hours, outreach services)
	
	Outcome 1:	Mobile clinic or outreach services - If (and how many) new mobile maternity clinics have been set-up in the last 3 years.
				vf05 vf06
	Outcome 2:	Build or request a new facility - If (and how many) new facilities have been built/commissioned in the last 3 years. 
				vf07, vf091-22
	Outcome 3:	New posyandu - If a new posyandu had been started, a physical posyandu structure had been built, or the posyandu cadre had been reactivated in the past 3 years
				vf04 vf04a
	Outcome 4:	Request ambulance - Whether the facility has a functional ambulance.
				amkp22 amkp23 amkp24 amkp25
	
Category 3. Improved information transparency (cost, opening hours, etc.) or complaint mechanisms

	Outcome 1:	Cost transparency - If cost information for delivery and other services is displayed publically.
				pi06, pi06a
	Outcome 2:	Hours transparency - If information on facility operating hours is displayed publically.
				kf09
	Outcome 3:	Complaint mechanism - 
				i. If the facility has a complaint management system
				pi09 pi10
				ii. If the facility conducts routine meetings with community members in the service area to identify how to improve quality of services
				pi11 pi12
				
Category 4. Increased availability of drugs, supplies and other inputs
	
	Outcome 1:	Number of essential medicines/equipment for mothers that the facility has in stock.
				ks03A-ks03P
	Outcome 2:	Number of essential medicines/equipment for children that the facility has in stock.
				ks04A-ks04N
	Outcome 3:	Number of essential vaccines that the facility has in stock.
				ks05A-ks05F
	Outcome 4:	Number of essential supplies that the facility has in stock.
				ks06A-ks06H
	Outcome 5:	Number of essential equipment that the facility has in stock.
				ks07A-ks07O

Category 6. Improved facility infrastructure

	Outcome 1:	Electricity
				amkp01 amkp02 amkp03 amkp04 amkp05 amkp06 amkp06a
	Outcome 2:	Telecommunications and computer/internet
				amkp11 amkp12 amkp13 amkp14 amkp15 amkp16 amkp17 amkp18 amkp19 amkp20 amkp21
	Outcome 3:	Water
				amkp08 amkp09 amkp10 amkp10_x
	Outcome 4:	Delivery room
				amkp26 amkp27 amkp27_1 amkp27_2 amkp28 amkp28_ot amkp29 amkp30
	Outcome 5:	Toilet
				amkp33 amkp33_ot amkp34_a amkp34_b amkp34_c amkp34_d amkp34_e amkp35
	Outcome 6:	Communication			
				amkp36 amkp37 amkp37_ot
				
Category 7. Increased or improved facility staffing

	Outcome 1:	Number of staff in the MNH and birth unit
				sd02 sd02a
	Outcome 2:	Vacancies for medical staff in the MNH and birth unit
				sd03_a
	Outcome 3:	Vacancies for other staff in the MNH and birth unit
				sd03_b

Category 8. Bylaws, partnerships, or other interventions aimed at health system uptake

	Outcome 1:	By-laws
				pi14 pi17 pi20 pi21
	Outcome 2:	Midwife-TBA partnerships - If there are midwife partnerships with baby dukun or TBAs
				pi08a

Category 9. Improved facility cleanliness
	
	Outcome 1:	Observed level of cleanliness of the facility delivery room and toilet 
				amkp31* amkp34*

Category 10. Improved attitude, effort, trust of the provider

	Outcome 1:	Provider perception of community problems
				pp04 pp05 pp06 pp07
	Outcome 2:	Provider perception of community feedback and information levels
				pp12 pp13 pp14 pp15 pp16 pp17
	Outcome 3:	Midwife residence in village
				vf01 vf02 vf03
				
Category 11. Improved provider knowledge

	Outcome 1:	Number of questions the midwife can correctly answer questions on healthcare practices.
				pp01 pp02 pp03
				
*/

clear all
set more off
	
u "$data/facility_data.dta", clear
*drop if fas_fascode == "11411"	//Dropping Puskesmas Ulugalung (see note above)
	
*******************************************************************************

* CATEGORY 2. Improved facility access

***Preparing Outcome 1***
g mobile_clinic = (vf05 == 1)
replace mobile_clinic = . if vf05 == 9

g mobile_clinic_recent = (vf06 == 1)
replace mobile_clinic_recent = . if vf05 == 9


***Preparing Outcome 2***

	//Counting the number of facilities built in the catchment area in the last three years
local vf09 vf091 vf092 vf093 vf094 vf095 vf096 vf097 vf098 vf099 vf0910 vf0911 vf0912 vf0913 vf0914 vf0915 vf0916 vf0917 vf0918 vf0919 vf0920 vf0921 vf0922
foreach var of local vf09 {
	g `var'_dummy = (`var' == 2)
	replace `var'_dummy = . if `var' == .
}
egen number_facilities_built = rowtotal(vf091_dummy-vf0922_dummy)
tab number_facilities_built
drop *_dummy


***Preparing Outcome 3***
g posyandu_recent = (vf04a == 1)
ren vf04 number_posyandu


***Preparing Outcome 4***
replace amkp22 = 0 if amkp22 == 3
replace amkp24 = 0 if amkp24 == 3
replace amkp25 = 0 if amkp25 == 3

count if amkp22 == 0 & amkp25 == 1 
	//Five cases where the puskesmas doesn't have an ambulance
	//Of those five, just one case where the puskesmas has an ambulance stationed elsewhere
	//As noted below, amkp25 dropped from analysis

	//The number of observed/unobserved cases does not differ across treatment & control
	//Hence, we don't need to adjust numbers for enumerator observation/reported not seen
	reg amkp23 treatment, vce(robust)

***Output Tables***

*Label variables
lab var mobile_clinic "Mobile clinic serves sample village"
lab var mobile_clinic_recent "Mobile clinic serves sample village & setup within past 3 years"
lab var number_facilities_built "Number of facilities built in the last 3 years"
lab var posyandu_recent "New posyandus have bee setup in the last 3 years"
lab var number_posyandu "Number of posyandu located in sample village"
lab var amkp22 "Facility has functional ambulance"
lab var amkp24 "Fuel for ambulance available today"

*Produce Excel output
cd "$analysis"

//Create the table shell
putexcel set "intermediate_outcomes.xlsx", sheet("Category 2 - Facility") modify

putexcel A1=("Outcome") B1=("Treatment Mean") C1=("Control Mean") D1=("Impact") ///
E1=("p-value") F1=("Effect Size") G1=("Sample Size") A10=("Number of Respondents") ///
A11=("Number of villages") B10=(100) C10=(100) B11=(100) C11=(100) ///
A13=("Treatment means are regression adjusted") A14=("*** p<0.01, ** p<0.05, * p<0.1")

//Export variable label, regression coefficient, control mean, treatment mean, p-value, sample size
local outcomes mobile_clinic mobile_clinic_recent number_facilities_built ///
posyandu_recent number_posyandu amkp22 amkp24
local row = 2
foreach var of local outcomes {
	sleep 2000
	local varlabel : var label `var'
	
	qui reg `var' treatment strata1 strata2 strata3, vce(robust)
	
	local p = 2*ttail(e(df_r), abs(_b[treatment]/_se[treatment]))
	local stars
		if `p' < 0.10 local stars = "*" 
		if `p' < 0.05 local stars = "**" 
		if `p' < 0.01 local stars = "***"
	local impact = _b[treatment]
	
	qui sum `var' if treatment == 0
	putexcel A`row' = ("`varlabel'")
	putexcel B`row' = ((_b[treatment]) + (r(mean)))
	putexcel C`row' = (r(mean))
	putexcel D`row' = ("`impact'" + "`stars'")
	putexcel E`row' = (`p')
	putexcel F`row' = (_b[treatment]/r(sd))
	putexcel G`row' = (e(N))
	local ++row
}

*******************************************************************************

* CATEGORY 3. Improved information transparency (cost, opening hours, etc.) or complaint mechanisms

***Preparing Outcome 1***
tab pi06
replace pi06 = . if pi06 == 6 //Facilities with no delivery unit are excluded from this analysis
tab pi06a


***Preparing Outcome 2***
tab kf09
ren kf09 hours_transparency

***Preparing Outcome 3***
tab pi09
tab pi10	//Type of feedback mechanism -- this is more for descriptive purposes
ren pi09 complaint_system

tab pi11 
tab pi12	
l fas_fascode if pi12 > 100 & pi12 != . 
			/*
			Three outliers - Facility IDs are 02711, 02911, 03921
			These outliers are entry errors, confirmed by Amalia (SurveyMeter)
			in an email sent on July 3, 2018. 
			Replacing these entry errors with the correct values provided by Amalia.
			*/
replace pi12 = 72 if fas_fascode == "02711"
replace pi12 = 96 if fas_fascode == "02911"
replace pi12 = 3 if fas_fascode == "03911"

g number_community_meetings = pi12
replace number_community_meetings = 0 if pi11 == 3			

***Output Tables***

*Label variables
lab var pi06 "Information on cost of delivery services displayed"
lab var pi06a "Information on cost of other services displayed"
lab var hours_transparency "Information on facility hours displayed"
lab var complaint_system "Presence of complaint management system"
lab var number_community_meetings "Number of meetings with community members in the past year"


*Produce Excel output

//Create the table shell
putexcel set "intermediate_outcomes.xlsx", sheet("Category 3") modify

putexcel A1=("Outcome") B1=("Treatment Mean") C1=("Control Mean") D1=("Impact") ///
E1=("p-value") F1=("Effect Size") G1=("Sample Size") A9=("Number of Respondents") ///
A10=("Number of villages") B9=(100) C9=(100) B10=(100) C10=(100) ///
A12=("Treatment means are regression adjusted") A13=("*** p<0.01, ** p<0.05, * p<0.1")

//Export variable label, regression coefficient, control mean, treatment mean, p-value, sample size
//Effect sizes are exported separately (see below)
local outcomes pi06 pi06a hours_transparency complaint_system number_community_meetings
local row = 2
foreach var of local outcomes {
	sleep 2000
	local varlabel : var label `var'
	
	qui reg `var' treatment strata1 strata2 strata3, vce(robust)
	
	local p = 2*ttail(e(df_r), abs(_b[treatment]/_se[treatment]))
	local stars
		if `p' < 0.10 local stars = "*" 
		if `p' < 0.05 local stars = "**" 
		if `p' < 0.01 local stars = "***"
	local impact = _b[treatment]
	
	qui sum `var' if treatment == 0
	putexcel A`row' = ("`varlabel'")
	putexcel B`row' = ((_b[treatment]) + (r(mean)))
	putexcel C`row' = (r(mean))
	putexcel D`row' = ("`impact'" + "`stars'")
	putexcel E`row' = (`p')
	putexcel G`row' = (e(N))
	local ++row
}

//Export effect sizes for outcomes that aren't already standardized
local outcomes pi06 pi06a hours_transparency complaint_system number_community_meetings
local row = 2
foreach var of local outcomes {
	qui reg `var' treatment strata1 strata2 strata3, vce(robust)
	qui sum `var' if treatment == 0
	putexcel F`row' = (_b[treatment]/r(sd))
	local ++row
}

*******************************************************************************

* CATEGORY 4. Increased availability of drugs, supplies and other inputs

***Preparing Outcome 1***
local ks03 ks03A ks03B ks03C ks03D ks03E ks03F ks03G ks03H ks03I ks03J ks03K ks03L ks03M ks03N ks03O ks03P
foreach var of local ks03 {
	g `var'_dummy = (`var' == 1)
	replace `var'_dummy = . if `var' == .
}

egen number_medicines_mother = rowtotal(ks03A_dummy-ks03P_dummy)
tab number_medicines_mother
drop *_dummy

***Preparing Outcome 2***
local ks04 ks04A ks04B ks04C ks04D ks04E ks04F ks04G ks04H ks04I ks04J ks04K ks04L ks04M ks04N
foreach var of local ks04 {
	g `var'_dummy = (`var' == 1)
	replace `var'_dummy = . if `var' == .
}

egen number_medicines_child = rowtotal(ks04A_dummy-ks04N_dummy)
tab number_medicines_child
drop *_dummy

***Preparing Outcome 3***
local ks05 ks05A ks05B ks05C ks05D ks05E ks05F
foreach var of local ks05 {
	g `var'_dummy = (`var' == 1)
	replace `var'_dummy = . if `var' == .
}

egen number_vaccines = rowtotal(ks05A_dummy-ks05F_dummy)
tab number_vaccines
drop *_dummy

***Preparing Outcome 4***
local ks06 ks06A ks06B ks06C ks06D ks06E ks06F ks06G ks06H
foreach var of local ks06 {
	g `var'_dummy = (`var' == 1)
	replace `var'_dummy = . if `var' == .
}

egen number_equipment = rowtotal(ks06A_dummy-ks06H_dummy)
tab number_equipment
drop *_dummy

***Preparing Outcome 5***
local ks07 ks07A ks07B ks07C ks07D ks07E ks07F ks07G ks07H ks07I ks07J ks07K ks07L ks07M ks07N ks07O
foreach var of local ks07 {
	g `var'_dummy = (`var' == 1)
	replace `var'_dummy = . if `var' == .
}

egen number_supplies = rowtotal(ks07A_dummy-ks07O_dummy)
tab number_supplies
drop *_dummy

***Output Tables***

*Label variables
lab var number_medicines_mother "Number of essential medicines for mothers in stock"
lab var number_medicines_child "Number of essential medicines for children in stock"
lab var number_vaccines "Number of essential vaccines in stock"
lab var number_equipment "Number of essential equipment available & functional"
lab var number_supplies "Number of essential supplies in stock"

*Produce Excel output

//Create the table shell
putexcel set "intermediate_outcomes.xlsx", sheet("Category 4") modify

putexcel A1=("Outcome") B1=("Treatment Mean") C1=("Control Mean") D1=("Impact") ///
E1=("p-value") F1=("Effect Size") G1=("Sample Size") A8=("Number of Respondents") ///
A9=("Number of villages") B8=(100) C8=(100) B9=(100) C9=(100) ///
A11=("Treatment means are regression adjusted") A12=("*** p<0.01, ** p<0.05, * p<0.1")

//Export variable label, regression coefficient, control mean, treatment mean, p-value, sample size
//Effect sizes are exported separately (see below)
local outcomes number_medicines_mother number_medicines_child number_vaccines number_equipment number_supplies
local row = 2
foreach var of local outcomes {
	sleep 2000
	local varlabel : var label `var'
	
	qui reg `var' treatment strata1 strata2 strata3, vce(robust)
	
	local p = 2*ttail(e(df_r), abs(_b[treatment]/_se[treatment]))
	local stars
		if `p' < 0.10 local stars = "*" 
		if `p' < 0.05 local stars = "**" 
		if `p' < 0.01 local stars = "***"
	local impact = _b[treatment]
	
	qui sum `var' if treatment == 0
	putexcel A`row' = ("`varlabel'")
	putexcel B`row' = ((_b[treatment]) + (r(mean)))
	putexcel C`row' = (r(mean))
	putexcel D`row' = ("`impact'" + "`stars'")
	putexcel E`row' = (`p')
	putexcel G`row' = (e(N))
	local ++row
}

//Export effect sizes for outcomes that aren't already standardized
local outcomes number_medicines_mother number_medicines_child number_vaccines number_equipment number_supplies
local row = 2
foreach var of local outcomes {
	qui reg `var' treatment strata1 strata2 strata3, vce(robust)
	qui sum `var' if treatment == 0
	putexcel F`row' = (_b[treatment]/r(sd))
	local ++row
}

********************************************************************************

* CATEGORY 6. Improved attitude, effort, trust of the provider

***Preparing Outcome 1***
replace amkp02 = 0 if amkp02 == 2
replace amkp03 = 0 if amkp03 == 3
replace amkp05 = 0 if amkp05 == 3
replace amkp06 = 0 if amkp06 == 3
replace amkp06a = 0 if amkp06a == 3

***Preparing Outcome 2***
g functional_telephone = (amkp11 == 1 & amkp13 == 1)
replace functional_telephone = . if amkp11 == . & amkp13 == .

g functional_radio = (amkp14 == 1 & amkp16 == 1)
replace functional_radio = . if amkp14 == . & amkp16 == .

g functional_email = (amkp20 == 1 & amkp21 == 1)
replace functional_email = . if amkp20 == . & amkp21 == .
	
	//The number of observed/unobserved cases does not differ across treatment & control
	//Hence, we don't need to adjust numbers for enumerator observation/reported not seen
	foreach var in amkp12 amkp15 amkp18 {
		reg `var' treatment, vce(robust)
	}

	
***Preparing Outcome 3***
g water_onsite = (amkp08 == 1)
g water_shortage = (amkp09 == 1)
replace amkp10 = 1 if ea_id == "166"	//Fixing error in data entry
g water_shortage_period = .
	replace water_shortage_period = amkp10 if amkp10_x == 1
	replace water_shortage_period = amkp10*30 if amkp10_x == 2
	replace water_shortage_period = amkp10*12*30 if amkp10_x == 3

	
***Preparing Outcome 4***
g specific_delivery_room = (amkp26 == 1)
ren amkp28 privacy_delivery_room
ren amkp29 number_delivery_bed


***Preparing Outcome 5***
g good_toilet = (amkp33 == 1 | amkp33 == 2 | amkp33 == 8 | amkp33 == 9)
replace good_toilet = . if amkp33 == .

g toilet_accessible = (amkp35 == 1)
replace toilet_accessible = . if amkp35 == .


***Preparing Outcome 6***
g mnh_staff_always_present = (amkp36 == 1)
replace mnh_staff_always_present = . if amkp36 == .
	//11 puskesmas don't handle deliveries. Treated as missing. 

g mnh_contact_mobilephone = strpos(amkp37, "A") > 0 
replace mnh_contact_mobilephone = . if amkp37 == ""
replace mnh_contact_mobilephone = . if amkp37 == "W"

	/*
	Just 8 observations on mnh_contact_mobilephone - 181 of 189 facilities report
	that a health worker is always present at the facility. Of the remaining 8,
	7 report that the health worker can be contact via cellphone.
	No variation. Dropped from analysis.
	*/

***Output Tables***

*Label variables
lab var amkp02 "Uninterrupted electricity during opening hours in last 7 days"
lab var amkp03 "Facility has other sources of electricity besides central grid"
lab var amkp05 "Facility generator is functional"
lab var amkp06 "Fuel available for generator"
lab var amkp06a "Facility solar power system is functional"
lab var functional_telephone "Facility has functional telephone" 
lab var functional_radio "Facility has functional radio"
lab var functional_email "Facility has functional email"
lab var water_onsite "Water outlet available onsite"
lab var water_shortage "Routine water shortage"
lab var water_shortage_period "Duration of water shortage (in days)"
lab var specific_delivery_room "Facility has specific room for deliveries" 
lab var privacy_delivery_room "Privacy level of delivery room (1-4 scale)"
lab var number_delivery_bed "Number of designated delivery beds"
lab var good_toilet "Delivery patient toilet: Flushed to septic tank/through piped system"
lab var toilet_accessible "Toilet inside/next to delivery room"
lab var mnh_staff_always_present "Skilled delivery staff present/on call 24*7"

*Produce Excel output

//Create the table shell
putexcel set "intermediate_outcomes.xlsx", sheet("Category 6") modify

putexcel A1=("Outcome") B1=("Treatment Mean") C1=("Control Mean") D1=("Impact") ///
E1=("p-value") F1=("Effect Size") G1=("Sample Size") A20=("Number of Respondents") ///
A21=("Number of villages") B20=(100) C20=(100) B21=(100) C21=(100) ///
A23=("Treatment means are regression adjusted") A24=("*** p<0.01, ** p<0.05, * p<0.1")

//Export variable label, regression coefficient, control mean, treatment mean, p-value, sample size
local outcomes amkp02 amkp03 amkp05 amkp06 amkp06a functional_telephone functional_radio functional_email ///
water_onsite water_shortage specific_delivery_room privacy_delivery_room number_delivery_bed ///
good_toilet toilet_accessible mnh_staff_always_present
local row = 2
foreach var of local outcomes {
	sleep 2000
	local varlabel : var label `var'
	
	qui reg `var' treatment strata1 strata2 strata3, vce(robust)
	
	local p = 2*ttail(e(df_r), abs(_b[treatment]/_se[treatment]))
	local stars
		if `p' < 0.10 local stars = "*" 
		if `p' < 0.05 local stars = "**" 
		if `p' < 0.01 local stars = "***"
	local impact = _b[treatment]
	
	qui sum `var' if treatment == 0
	putexcel A`row' = ("`varlabel'")
	putexcel B`row' = ((_b[treatment]) + (r(mean)))
	putexcel C`row' = (r(mean))
	putexcel D`row' = ("`impact'" + "`stars'")
	putexcel E`row' = (`p')
	putexcel F`row' = (_b[treatment]/r(sd))
	putexcel G`row' = (e(N))
	local ++row
}

//Exporting water_shortage_period separately to account for the outlier
	qui reg water_shortage_period treatment strata1 strata2 strata3 if water_shortage_period != 3600, vce(robust)

	local varlabel : var label water_shortage_period
	local p = 2*ttail(e(df_r), abs(_b[treatment]/_se[treatment]))
	local stars
		if `p' < 0.10 local stars = "*" 
		if `p' < 0.05 local stars = "**" 
		if `p' < 0.01 local stars = "***"
	local impact = _b[treatment]
	
	qui sum water_shortage_period if treatment == 0
	putexcel A18 = ("`varlabel'")
	putexcel B18 = ((_b[treatment]) + (r(mean)))
	putexcel C18 = (r(mean))
	putexcel D18 = ("`impact'" + "`stars'")
	putexcel E18 = (`p')
	putexcel F18 = (_b[treatment]/r(sd))
	putexcel G18 = (e(N))

********************************************************************************

* CATEGORY 7. Increased or improved facility staffing

desc sd02 sd02a sd03_a sd03_b

***Preparing Outcome 1***
g number_mnh_workers = sd02 + sd02a

***Preparing Outcomes 2 and 3***
g any_vacancies = (sd03_a > 0 | sd03_b > 0)
g vacancies_total = (sd03_a + sd03_b)

***Output Tables***

*Label variables
lab var number_mnh_workers "Total number of staff & resident midwives"
lab var sd03_a "Number of vacancies for medical staff"
lab var sd03_b "Number of vacancies for non-medical staff"

*Produce Excel output

//Create the table shell
putexcel set "intermediate_outcomes.xlsx", sheet("Category 7") modify

putexcel A1=("Outcome") B1=("Treatment Mean") C1=("Control Mean") D1=("Impact") ///
E1=("p-value") F1=("Effect Size") G1=("Sample Size") A7=("Number of Respondents") ///
A8=("Number of villages") B7=(100) C7=(100) B8=(100) C8=(100) ///
A10=("Treatment means are regression adjusted") A11=("*** p<0.01, ** p<0.05, * p<0.1")

//Export variable label, regression coefficient, control mean, treatment mean, p-value, sample size
//Effect sizes are exported separately (see below)
local outcomes number_mnh_workers sd03_a sd03_b 
local row = 2
foreach var of local outcomes {
	sleep 2000
	local varlabel : var label `var'
	
	qui reg `var' treatment strata1 strata2 strata3, vce(robust)
	
	local p = 2*ttail(e(df_r), abs(_b[treatment]/_se[treatment]))
	local stars
		if `p' < 0.10 local stars = "*" 
		if `p' < 0.05 local stars = "**" 
		if `p' < 0.01 local stars = "***"
	local impact = _b[treatment]
	
	qui sum `var' if treatment == 0
	putexcel A`row' = ("`varlabel'")
	putexcel B`row' = ((_b[treatment]) + (r(mean)))
	putexcel C`row' = (r(mean))
	putexcel D`row' = ("`impact'" + "`stars'")
	putexcel E`row' = (`p')
	putexcel G`row' = (e(N))
	local ++row
}

//Export effect sizes for outcomes that aren't already standardized
local outcomes number_mnh_workers sd03_a sd03_b
local row = 2
foreach var of local outcomes {
	qui reg `var' treatment strata1 strata2 strata3, vce(robust)
	qui sum `var' if treatment == 0
	putexcel F`row' = (_b[treatment]/r(sd))
	local ++row
}

*******************************************************************************

* CATEGORY 8. Bylaws, partnerships, or other interventions aimed at health system uptake

***Preparing Outcome 1***
local bylaws pi14 pi17 pi20
foreach var of local bylaws {
	replace `var' = 0 if `var' == 3
	tab `var'
}

tab pi21

***Preparing Outcome 2***
tab pi08a
tab pi07

replace pi08a = 0 if pi08a == 3
replace pi08a = 0 if (pi08a == . & pi07 == 3)
	
	/*
		pi08a has 17 missing values.
		
		11 of these are puskesmas that do not have a delivery unit.
		
		6 are puskesmas that answered "No" to pi07, i.e. there have been no new
		initiatives/strategies to increase the number of mothers who give birth
		in the facility in the past 3 years. 
		
		These 6 missing values are treated the same as a "No", and replaced
		accordingly.
		
	*/
	

***Output Tables***

*Label variables
lab var pi14 "Village has by-law on MNH services"
lab var pi17 "District Govt./Sub-district Govt./Puskesmas has by-law on MNH services"
lab var pi20 "Province/National Govt. has by-law on MNH services"
lab var pi08a "Puskesmas has partnership with Baby Dukun"


*Produce Excel output

//Create the table shell
putexcel set "intermediate_outcomes.xlsx", sheet("Category 8") modify

putexcel A1=("Outcome") B1=("Treatment Mean") C1=("Control Mean") D1=("Impact") ///
E1=("p-value") F1=("Effect Size") G1=("Sample Size") A7=("Number of Respondents") ///
A8=("Number of villages") B7=(100) C7=(100) B8=(100) C8=(100) ///
A10=("Treatment means are regression adjusted") A11=("*** p<0.01, ** p<0.05, * p<0.1")

//Export variable label, regression coefficient, control mean, treatment mean, p-value, sample size
//Effect sizes are exported separately (see below)
local outcomes pi14 pi17 pi20 pi08a
local row = 2
foreach var of local outcomes {
	sleep 2000
	local varlabel : var label `var'
	
	qui reg `var' treatment strata1 strata2 strata3, vce(robust)
	
	local p = 2*ttail(e(df_r), abs(_b[treatment]/_se[treatment]))
	local stars
		if `p' < 0.10 local stars = "*" 
		if `p' < 0.05 local stars = "**" 
		if `p' < 0.01 local stars = "***"
	local impact = _b[treatment]
	
	qui sum `var' if treatment == 0
	putexcel A`row' = ("`varlabel'")
	putexcel B`row' = ((_b[treatment]) + (r(mean)))
	putexcel C`row' = (r(mean))
	putexcel D`row' = ("`impact'" + "`stars'")
	putexcel E`row' = (`p')
	putexcel G`row' = (e(N))
	local ++row
}

//Export effect sizes for outcomes that aren't already standardized
local outcomes pi14 pi17 pi20 pi08a
local row = 2
foreach var of local outcomes {
	qui reg `var' treatment strata1 strata2 strata3, vce(robust)
	qui sum `var' if treatment == 0
	putexcel F`row' = (_b[treatment]/r(sd))
	local ++row
}

*******************************************************************************

* CATEGORY 9. Improved facility cleanliness

ren amkp31_a delivery_room_bed
ren amkp31_b delivery_room_floor
ren amkp31_c delivery_room_ventilation
ren amkp31_d delivery_room_waste_disposal
ren amkp31_e delivery_room_dust

ren amkp34_a toilet_water_flush
ren amkp34_b toilet_water_handwash
ren amkp34_c toilet_soap_handwash
ren amkp34_d toilet_cleanliness
ren amkp34_e toilet_privacy

***Preparing delivery room outcome***
replace delivery_room_bed = 0 if (delivery_room_bed == 2 | delivery_room_bed == 3)
replace delivery_room_floor = 0 if (delivery_room_floor == 2 | delivery_room_floor == 3)
	//Creating a dirty/clean binary variable for delivery_room_floor

replace delivery_room_ventilation = 0 if delivery_room_ventilation == 3
replace delivery_room_ventilation = 1 if delivery_room_ventilation == 6	
	//If the room is air conditioned, it is regarded as well-ventilated

replace delivery_room_dust = 0 if delivery_room_dust == 1
replace delivery_room_dust = 1 if delivery_room_dust == 3
	//Orienting the variable so that "1" represents the positive outcome

replace delivery_room_waste_disposal = 0 if delivery_room_waste_disposal == 3

***Preparing toilet outcome***
replace toilet_water_flush = 0 if toilet_water_flush == 3
replace toilet_water_handwash = 0 if toilet_water_handwash == 3
replace toilet_soap_handwash = 0 if toilet_soap_handwash == 3

***Output Tables***

*Label variables
lab var delivery_room_bed "Delivery bed is clean"
lab var delivery_room_floor "Delivery room floor is clean"
lab var delivery_room_ventilation "Delivery room is well ventilated"
lab var delivery_room_waste_disposal "Biological/medical waste disposal available"
lab var delivery_room_dust "No dust or mold observed in the room"
lab var toilet_water_flush "Delivery patient toilet - Water is available to flush"
lab var toilet_water_handwash "Delivery patient toilet - Water to wash hands"
lab var toilet_soap_handwash "Delivery patient toilet - Soap to wash hands"
lab var toilet_cleanliness "Delivery patient toilet - Cleanliness (1-3 scale)"
lab var toilet_privacy "Delivery patient toilet - Privacy (1-3 scale)"

*Produce Excel output
cd "$analysis"

//Create the table shell
putexcel set "intermediate_outcomes.xlsx", sheet("Category 9_facility") modify

putexcel A1=("Outcome") B1=("Treatment Mean") C1=("Control Mean") D1=("Impact") ///
E1=("p-value") F1=("Effect Size") G1=("Sample Size") A14=("Number of Respondents") ///
A15=("Number of villages") B14=(100) C14=(100) B15=(100) C15=(100) ///
A17=("Treatment means are regression adjusted") A18=("*** p<0.01, ** p<0.05, * p<0.1")

//Export variable label, regression coefficient, control mean, treatment mean, p-value, sample size
local outcomes delivery_room_bed delivery_room_floor delivery_room_ventilation delivery_room_waste_disposal ///
delivery_room_dust toilet_water_flush toilet_water_handwash toilet_soap_handwash toilet_cleanliness toilet_privacy
local row = 2
foreach var of local outcomes {
	sleep 2000
	local varlabel : var label `var'
	
	qui reg `var' treatment strata1 strata2 strata3, vce(robust)
	
	local p = 2*ttail(e(df_r), abs(_b[treatment]/_se[treatment]))
	local stars
		if `p' < 0.10 local stars = "*" 
		if `p' < 0.05 local stars = "**" 
		if `p' < 0.01 local stars = "***"
	local impact = _b[treatment]
	
	qui sum `var' if treatment == 0
	putexcel A`row' = ("`varlabel'")
	putexcel B`row' = ((_b[treatment]) + (r(mean)))
	putexcel C`row' = (r(mean))
	putexcel D`row' = ("`impact'" + "`stars'")
	putexcel E`row' = (`p')
	putexcel F`row' = (_b[treatment]/r(sd))
	putexcel G`row' = (e(N))
	local ++row
}

*******************************************************************************

* CATEGORY 10. Improved attitude, effort, trust of the provider

***Outcome 1***

	//No regressions to run for this outcome - just tabulation.
tab pp04
tab pp05
tab pp06, sort
tab pp07, sort

***Preparing outcome 2***
foreach var in pp12 pp13 pp14 pp15 pp16 pp17 {
	replace `var' = . if `var' == 97
}

***Preparing outcome 3***
foreach var in vf01 vf02 vf03 {
	replace `var' = 0 if `var' == 3
}

***Output Tables***

*Label variables
lab var pp12 "Women are well aware of the MNH services offered at [puskesmas name]"
lab var pp13 "Women seeking MNH services understand the problems in running [puskesmas name]"
lab var pp14 "When [puskesmas] is making a decision about MNH, community members are consulted"
lab var pp15 "In general, maternal health patients do not have meaningful feedback"
lab var pp16 "Most patients don’t appreciate the effort put in by the staff"
lab var pp17 "Patients communicate about their feedback on MNH services"
lab var vf01 "Midwife assigned to sample village"
lab var vf02 "Assigned midwife lives in sample village"
lab var vf03 "Midwife receives free housing in village"

*Produce Excel output

//Create the table shell
putexcel set "intermediate_outcomes.xlsx", sheet("Category 10_facility") modify

putexcel A1=("Outcome") B1=("Treatment Mean") C1=("Control Mean") D1=("Impact") ///
E1=("p-value") F1=("Effect Size") G1=("Sample Size") A13=("Number of Respondents") ///
A14=("Number of villages") B13=(100) C13=(100) B14=(100) C14=(100) ///
A16=("Treatment means are regression adjusted") A17=("*** p<0.01, ** p<0.05, * p<0.1")

//Export variable label, regression coefficient, control mean, treatment mean, p-value, sample size
local outcomes pp12 pp13 pp14 pp15 pp16 pp17 vf01 vf02 vf03
local row = 2
foreach var of local outcomes {
	sleep 2000
	local varlabel : var label `var'
	
	qui reg `var' treatment strata1 strata2 strata3, vce(robust)
	
	local p = 2*ttail(e(df_r), abs(_b[treatment]/_se[treatment]))
	local stars
		if `p' < 0.10 local stars = "*" 
		if `p' < 0.05 local stars = "**" 
		if `p' < 0.01 local stars = "***"
	local impact = _b[treatment]
	
	qui sum `var' if treatment == 0
	putexcel A`row' = ("`varlabel'")
	putexcel B`row' = ((_b[treatment]) + (r(mean)))
	putexcel C`row' = (r(mean))
	putexcel D`row' = ("`impact'" + "`stars'")
	putexcel E`row' = (`p')
	putexcel F`row' = (_b[treatment]/r(sd))
	putexcel G`row' = (e(N))
	local ++row
}

*******************************************************************************

* CATEGORY 11. Improved provider knowledge

***Preparing outcomes***
g anc_right_answer = (pp01 == 4)
g facility_birth_right_answer = (pp02 == 4)

foreach value in A B C D E F {
	g pp03_`value' = strpos(pp03, "`value'") > 0 
}
egen reason_refer_hospital = rowtotal(pp03_A-pp03_F), missing
drop pp03_A-pp03_F

***Output Tables***

*Label variables
lab var anc_right_answer "Provider responded correctly to ANC question"
lab var facility_birth_right_answer "Provider responded correctly to delivery question"
lab var reason_refer_hospital "Number of reasons to refer woman to hospital for birth"

*Produce Excel output

//Create the table shell
putexcel set "intermediate_outcomes.xlsx", sheet("Category 11") modify

putexcel A1=("Outcome") B1=("Treatment Mean") C1=("Control Mean") D1=("Impact") ///
E1=("p-value") F1=("Effect Size") G1=("Sample Size") A6=("Number of Respondents") ///
A7=("Number of villages") B6=(100) C6=(100) B7=(100) C7=(100) ///
A9=("Treatment means are regression adjusted") A10=("*** p<0.01, ** p<0.05, * p<0.1")

//Export variable label, regression coefficient, control mean, treatment mean, p-value, sample size
//Effect sizes are exported separately (see below)
local outcomes anc_right_answer facility_birth_right_answer reason_refer_hospital
local row = 2
foreach var of local outcomes {
	sleep 2000
	local varlabel : var label `var'
	
	qui reg `var' treatment strata1 strata2 strata3, vce(robust)
	
	local p = 2*ttail(e(df_r), abs(_b[treatment]/_se[treatment]))
	local stars
		if `p' < 0.10 local stars = "*" 
		if `p' < 0.05 local stars = "**" 
		if `p' < 0.01 local stars = "***"
	local impact = _b[treatment]
	
	qui sum `var' if treatment == 0
	putexcel A`row' = ("`varlabel'")
	putexcel B`row' = ((_b[treatment]) + (r(mean)))
	putexcel C`row' = (r(mean))
	putexcel D`row' = ("`impact'" + "`stars'")
	putexcel E`row' = (`p')
	putexcel G`row' = (e(N))
	local ++row
}

//Export effect sizes for outcomes that aren't already standardized
local outcomes anc_right_answer facility_birth_right_answer reason_refer_hospital
local row = 2
foreach var of local outcomes {
	qui reg `var' treatment strata1 strata2 strata3, vce(robust)
	qui sum `var' if treatment == 0
	putexcel F`row' = (_b[treatment]/r(sd))
	local ++row
}

*******************************************************************************

clear


	
